package com.whmnx.chitchat.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.whmnx.chitchat.utils.EmptyListUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 *
 * @author Administrator
 */
public abstract class BaseFragment extends Fragment {
    protected View rootView;
    private Unbinder bind;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutResource(), container, false);
        }
       ViewGroup parent = (ViewGroup) rootView.getParent();
       if (parent != null) {
            parent.removeAllViews();
        }
       // container.addView(rootView);
        bind = ButterKnife.bind(this, rootView);
        return rootView;
}

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    protected abstract int getLayoutResource();

    public String getName() {
        return BaseFragment.class.getName();
    }

        protected void changeFragment(Fragment fragment, Bundle args) {
        // 判断传递参数是否为空,如果不为空,设置到fragment上
        if (args != null) {
            fragment.setArguments(args);
        }
    }

    public void showEmpty(boolean isShowEmpty, ViewStub mVsEmpty, int emptyType) {
        if (isShowEmpty) {
            EmptyListUtils.loadEmpty(true, mVsEmpty,emptyType);
        } else {
            if (mVsEmpty != null) {
                mVsEmpty.setVisibility(View.GONE);
            }
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消请求
    //    HttpClient.cancelRequest(this);
        if (bind != null) {
            bind.unbind();
        }

       if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
    }
}
